<template>
	<view>
		<u-navbar title="上课模式" :autoBack="true" rightText="添加" @rightClick="rightClick">
		</u-navbar>
		<view class="u-border-top">
			<u-swipe-action :autoClose="true">
				<u-swipe-action-item :options="options" @click="del(item.class_mode_id)" v-for="(item, index) in list" :key="item.class_mode_id">
					<view class="swipe-action u-border-bottom flex align-center justify-between px-3">
						<view class="swipe-action__content">
							<view class="font36 font-weight-bold mb-1  line-h">{{item.start_time}}-{{item.end_time}}
							</view>
							<view class="font24 line-h color666">
								周<text v-for="(items,index1) in item.weeks" :key="index1">{{items}}{{(index1==(item.weeks.length -1)) ?'': '、'}}</text>
							</view>
						</view>
						<view class="">
							<u-switch v-model="item.enabled" @change="change(item)" size="22"></u-switch>
						</view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
		</view>
		<u-loading-page :loading="loadingPage" loading-text="修改中..." bg-color="rgba(000, 000, 000, 0.5)">
		</u-loading-page>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadingPage: false,
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				time: "",
				timeData: [{
						name: '日',
						id: 0
					}, {
						name: '一',
						id: 1
					},
					{
						name: '二',
						id: 2
					},
					{
						name: '三',
						id: 3
					},
					{
						name: '四',
						id: 4
					},
					{
						name: '五',
						id: 5
					},
					{
						name: '六',
						id: 6
					}
				],
				list: []
			};
		},
		onShow() {
			this.getData()
		},
		methods: {
			rightClick() {
				uni.navigateTo({
					url: `./addmode/addmode`
				})
			},
			change(data) {
				let _this = this;
				_this.loadingPage = true;
				let opts = {
					url: 'Jz/SaveShmsJz.shtml'
				};
				let param = {
					class_code: uni.getStorageSync('classcode'),
					class_mode_id: data.class_mode_id,
					start_time: data.start_time,
					end_time: data.end_time,
					enabled: data.enabled, 
					week_days: data.week_days,
					xsId:uni.getStorageSync('classUserId')
				};
				_this.$http.httpRequest(opts, param).then(
					res => {
						_this.loadingPage = false;
						console.log(res.data);
						if (res.data.code == '200') {
							uni.showToast({
								title: "修改成功",
								icon: "none"
							})
						}
					},
					error => {
						console.log(error);
					}
				)
			},
			getData() {
				let _this = this;
				_this.loadingPage = true;
				let opts = {
					url: 'Jz/SearchShmsJz.shtml',
				};
				let param = {
					xsId:uni.getStorageSync('classUserId')
				};
				_this.$http.httpRequest(opts, param).then(
					res => {
						console.log(res.data);
						_this.loadingPage = false;
						if (res.data.code == "200") {
							let arr = res.data.data;
							arr.forEach((item) => {
								item.weeks = [];
								item.week_days.split(',').forEach((items) => {
									this.timeData.forEach((itema) => {
										if (items == itema.id) {
											item.weeks.push(itema.name)
										}
									})
								})
							})
							console.log(arr)
							_this.list = arr
						}
					},
					error => {
						console.log(error);
					}
				);
			},
			del(id) {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: "是否删除该上课模式？",
					success: function(res) {
						if (res.confirm) {
							let opts = {
								url: 'Jz/deleteShmsJz.shtml',
							};
							let param = {
								class_mode_id: id
							};
							_this.$http.httpRequest(opts, param).then(
								res => {
									if (res.data.code == "200") {
										uni.showToast({
											title: "删除成功",
											icon: "none"
										})
										_this.getData()
									} else {
										uni.showToast({
											title: "删除失败",
											icon: "none"
										})
									}
								},
								error => {
									uni.showToast({
										title: "删除失败",
										icon: "none"
									})
								}
							);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}
		}
	}
</script>

<style lang="scss">
	.swipe-action {
		height: 120rpx;
	}
</style>
